$(document).ready(function(){
	
	$("#cnSubmit").removeAttr("disabled");
	$("#enSubmit").removeAttr("disabled");
	$("#wkeySubmit").removeAttr("disabled");
	$("#keywordSubmit").removeAttr("disabled");
	
	// 提交表单时，确认域名名称的合法性，确认所选后缀
	$("#cnDomainform").submit(function(){
		var type = "cn";
		var target = $("#cnDomain");
		
		var result = check(type, target);
		if(!result) return false;
	});
	
	// 提交表单时，确认域名名称的合法性，确认所选后缀
	$("#enDomainform").submit(function(){
		var type = "en";
		var target = $("#enDomain");
		
		var result = check(type, target);
		if(!result) return false;
	});
	
	// 提交表单时，确认无线网址的合法性
	$("#wkeyForm").submit(function(){
		var type = "wkey";
		var target = $("#wkey");
		var result = check(type, target);
		if(!result) return false;
	});
	
	// 提交表单时，确认无线网址的合法性
	$("#keywordForm").submit(function(){
		var type = "keyword";
		var target = $("#keyword");
		var result = check(type, target);
		if(!result) return false;
	});
		
});


function check(type, target){
	
	var tip = "域名";
	if(type == "wkey") tip = "无线网址";
	if(type == "keyword") tip = "通用网址";
	
	var value = $(target).val();
	if(!value){
		alert(tip + "名称不能为空，请填写");
		$(target).focus();
		return false;
	}
	
	var values;
	if(value.indexOf("\r\n") != -1){
		values = value.split("\r\n");
	}else{
		values = value.split("\n");
	}
	
	var length = values.length;
	for(var i=0; i<values.length; i++){
		var name = trim(values[i]);
		if(name == ''){
			length = length - 1;
		}else{
			var result = true;
			if(type == 'en'){
				result = /^[0-9a-zA-Z-]{1,60}$/.test(name);
			}
			if(type == 'cn'){
				if(name.length > 20) result = false;
				else result = /^([a-zA-Z0-9-]*[\u4e00-\u9fa5]+[a-zA-Z0-9-]*){1,}$/.test(name);
			}
			if(type == 'wkey'){
				result = /^[0-9a-zA-Z\u4e00-\u9fa5]{1,31}$/.test(name);
			}
			if(type == 'keyword'){
				result = /^[0-9a-zA-Z\u4e00-\u9fa5\-\!]{1,31}$/.test(name);
			}
			if(!result){
				alert("请输入正确的" +tip+ "名称");
				$(target).focus();
				return false;
			}
		}
	}
	
	if(length < 1){
		alert(tip + "名称不能为空，请填写");
		$(target).focus();
		return false;
	}
	
	if(length > 10){
		alert("最多只能查询10个" +tip);
		$(target).focus();
		return false;
	}
	
	if(type == "en" || type == "cn"){
		var length = $("input[type='checkbox'][class='" + type +"']:checked").length
		if(length < 1){
			alert("请至少选择一种后缀");
			$(target).focus();
			return false;
		}
	}
	
	// 禁用按钮
	$("#" + type + "Submit").attr("disabled", "disabled");
	return true;
	
}
